<template>
  <div class="h5-details">
    <div class="details-card">
      <div class="card-header" @click="isShow = !isShow">
        <span class="title">基本信息</span>
        <el-icon class="arrow-icon" :class="{ 'expanded': isShow }">
          <ArrowDown />
        </el-icon>
      </div>
      
      <el-collapse-transition>
        <div v-show="isShow" class="details-content">
          <!-- 客户名称 -->
          <div class="detail-item">
            <span class="label">客户名称</span>
            <span class="value">{{ CustomerValue.name || '-' }}</span>
          </div>
          
          <!-- 客户来源 -->
          <div class="detail-item">
            <span class="label">客户来源</span>
            <span class="value">
              <dict-tag :type="DICT_TYPE.CRM_CUSTOMER_SOURCE" :value="CustomerValue.source" />
            </span>
          </div>
          
          <!-- 手机 -->
          <div 
            class="detail-item clickable" 
            v-if="CustomerValue.mobile"
            @click="onCall(CustomerValue, CustomerValue.mobile)"
          >
            <span class="label">手机</span>
            <span class="value phone-value">
              {{ CustomerValue.encryptMobile }}
              <el-icon class="phone-icon"><Phone /></el-icon>
            </span>
          </div>
          
          <!-- 电话 -->
          <div 
            class="detail-item clickable" 
            v-if="CustomerValue.telephone"
            @click="onCall(CustomerValue, CustomerValue.telephone)"
          >
            <span class="label">电话</span>
            <span class="value phone-value">
              {{ CustomerValue.encryptTelephone }}
              <el-icon class="phone-icon"><Phone /></el-icon>
            </span>
          </div>
          
          <!-- 邮箱 -->
          <div class="detail-item">
            <span class="label">邮箱</span>
            <span class="value">{{ CustomerValue.email || '-' }}</span>
          </div>
          
          <!-- 地址 -->
          <div class="detail-item">
            <span class="label">地址</span>
            <span class="value">{{ `${CustomerValue.areaName || ''} ${CustomerValue.detailAddress || ''}`.trim() || '-' }}</span>
          </div>
          
          <!-- QQ -->
          <div class="detail-item">
            <span class="label">QQ</span>
            <span class="value">{{ CustomerValue.qq || '-' }}</span>
          </div>
          
          <!-- 微信 -->
          <div class="detail-item">
            <span class="label">微信</span>
            <span class="value">{{ CustomerValue.wechat || '-' }}</span>
          </div>
          
          <!-- 客户行业 -->
          <div class="detail-item">
            <span class="label">客户行业</span>
            <span class="value">
              <dict-tag :type="DICT_TYPE.CRM_CUSTOMER_INDUSTRY" :value="CustomerValue.industryId" />
            </span>
          </div>
          
          <!-- 客户级别 -->
          <div class="detail-item" v-if="CustomerValue.levelRespVO">
            <span class="label">客户级别</span>
            <span class="value">
              <el-tag
                :style="{
                  background: CustomerValue.levelRespVO.bgColor,
                  color: CustomerValue.levelRespVO.textColor,
                  border: 'none'
                }"
              >
                {{ CustomerValue.levelRespVO.name }}
              </el-tag>
            </span>
          </div>
          
          <!-- 下次联系时间 -->
          <div class="detail-item">
            <span class="label">下次联系时间</span>
            <span class="value">{{ formatDate(CustomerValue.contactNextTime) || '-' }}</span>
          </div>
          
          <!-- 备注 -->
          <div class="detail-item">
            <span class="label">备注</span>
            <span class="value">{{ CustomerValue.remark || '-' }}</span>
          </div>
          
          <!-- 自定义字段 -->
          <div 
            class="detail-item" 
            v-for="item in CustomerValue.detailFiled" 
            :key="item.id"
          >
            <span class="label">{{ item.name }}</span>
            <span class="value">{{ getFieldValue(item) }}</span>
          </div>
        </div>
      </el-collapse-transition>
    </div>
  </div>
</template>

<script setup>
import { DICT_TYPE } from '@/utils/dict'
import { formatDate } from '@/utils/formatTime'
import { useSoftPhoneStore } from '@/store/modules/softPhone'
import { usePhoneStore } from '@/store/modules/phone'
import { emitter } from '@/utils/eventBus.ts'
import { ArrowDown, Phone } from '@element-plus/icons-vue'

const useStore = usePhoneStore()
const useSoftPhone = useSoftPhoneStore()
const isShow = ref(true)

const props = defineProps({
  CustomerValue: Object
})

const onCall = (row, callee) => {
  emitter.emit('simCall', {
    callee,
    source: 'customer',
    sourceId: row.id
  })
  emitter.emit('userCallCallee', callee)
}

const getFieldValue = (item) => {
  if (item.fieldType === 'time') {
    return formatDate(props.CustomerValue[item.fieldName]) || '-'
  }
  return props.CustomerValue[item.fieldName] || '-'
}
</script>

<style lang="scss" scoped>
.h5-details {
  margin-bottom: 8px;
  
  .details-card {
    background: #fff;
    border-radius: 8px;
    overflow: hidden;
    
    .card-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 16px;
      background: #f8f9fa;
      cursor: pointer;
      border-bottom: 1px solid #ebeef5;
      
      .title {
        font-size: 16px;
        font-weight: 500;
        color: #303133;
      }
      
      .arrow-icon {
        transition: transform 0.3s;
        color: #909399;
        
        &.expanded {
          transform: rotate(180deg);
        }
      }
    }
    
    .details-content {
      padding: 0;
      
      .detail-item {
        display: flex;
        align-items: flex-start;
        padding: 12px 16px;
        border-bottom: 1px solid #f0f0f0;
        
        &:last-child {
          border-bottom: none;
        }
        
        &.clickable {
          cursor: pointer;
          
          &:hover {
            background-color: #f5f7fa;
          }
        }
        
        .label {
          flex-shrink: 0;
          width: 80px;
          color: #909399;
          font-size: 14px;
          line-height: 1.5;
        }
        
        .value {
          flex: 1;
          color: #303133;
          font-size: 14px;
          line-height: 1.5;
          word-break: break-all;
          
          &.phone-value {
            color: #409eff;
            display: flex;
            align-items: center;
            gap: 4px;
            
            .phone-icon {
              font-size: 14px;
            }
          }
        }
      }
    }
  }
}
</style>